/*
 * UserTestPanel.cpp
 *
 *  Created on: 2012-11-3
 *      Author: tutu
 */

#include "UserTestPanel.h"
#include "../TContext.h"
#include <QMessageBox>

UserTestPanel::UserTestPanel(QWidget *parent) : QWidget(parent)
{
	_listButton = new QPushButton(this);
	_listButton->setText("List");
	_listButton->setGeometry(10, 10, 100, 30);
	connect(_listButton, SIGNAL(clicked()), this, SLOT(listButtonClicked()));

	_idEdit = new QLineEdit(this);
	_idEdit->setText("Id");
	_idEdit->setGeometry(10, 50, 150, 30);

	_loginNameEdit = new QLineEdit(this);
	_loginNameEdit->setText("Login Name");
	_loginNameEdit->setGeometry(10, 90, 150, 30);

	_loginPasswordEdit = new QLineEdit(this);
	_loginPasswordEdit->setText("Login Password");
	_loginPasswordEdit->setGeometry(10, 130, 150, 30);

	_nameEdit = new QLineEdit(this);
	_nameEdit->setText("Name");
	_nameEdit->setGeometry(10, 170, 150, 30);

	_emailEdit = new QLineEdit(this);
	_emailEdit->setText("Email");
	_emailEdit->setGeometry(10, 210, 150, 30);

	_roleIdEdit = new QLineEdit(this);
	_roleIdEdit->setText("Role Id");
	_roleIdEdit->setGeometry(10, 250, 150, 30);

	_activeCheckBox = new QCheckBox(this);
	_activeCheckBox->setText("Active");
	_activeCheckBox->setGeometry(10, 290, 100, 30);

	_createButton = new QPushButton(this);
	_createButton->setText("Create");
	_createButton->setGeometry(10, 330, 100, 30);
	connect(_createButton, SIGNAL(clicked()), this, SLOT(createButtonClicked()));

	_updateButton = new QPushButton(this);
	_updateButton->setText("Update");
	_updateButton->setGeometry(10, 370, 100, 30);
	connect(_updateButton, SIGNAL(clicked()), this, SLOT(updateButtonClicked()));

	_updateStatusButton = new QPushButton(this);
	_updateStatusButton->setText("Update Status");
	_updateStatusButton->setGeometry(10, 410, 100, 30);
	connect(_updateButton, SIGNAL(clicked()), this, SLOT(updateStatusButtonClicked()));

	_requester = TContext::instance()->requesterFactory()->createUserRequester();
	connect(_requester.data(), SIGNAL(requestFinished(bool, QList<TUserPointer>)), this, SLOT(requestFinished(bool, QList<TUserPointer>)));
	connect(_requester.data(), SIGNAL(createFinished(bool , TUserPointer)), this, SLOT(createFinished(bool , TUserPointer)));
	connect(_requester.data(), SIGNAL(updateFinished(bool , TUserPointer)), this, SLOT(updateFinished(bool , TUserPointer)));
}

UserTestPanel::~UserTestPanel()
{
	// TODO Auto-generated destructor stub
}

void UserTestPanel::listButtonClicked()
{
	_requester->requestAllUsers();
}

void UserTestPanel::createButtonClicked()
{
	if(_loginNameEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No login name.");
		return;
	}
	if(_loginPasswordEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No login password.");
		return;
	}
	if(_nameEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No name.");
		return;
	}
	if(_emailEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No email.");
		return;
	}
	if(_roleIdEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No role id.");
		return;
	}

	_requester->createUser(_loginNameEdit->text(), _loginPasswordEdit->text(), _nameEdit->text(), _emailEdit->text(), _roleIdEdit->text().toInt());
}

void UserTestPanel::updateButtonClicked()
{
	if(_idEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No id.");
		return;
	}
	if(_loginNameEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No login name.");
		return;
	}
	if(_loginPasswordEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No login password.");
		return;
	}
	if(_nameEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No name.");
		return;
	}
	if(_emailEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No email.");
		return;
	}
	if(_roleIdEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No role id.");
		return;
	}
	_requester->updateUser(_idEdit->text().toInt(), _loginNameEdit->text(), _loginPasswordEdit->text(), _nameEdit->text(), _emailEdit->text(), _roleIdEdit->text().toInt(), _activeCheckBox->isChecked());
}

void UserTestPanel::updateStatusButtonClicked()
{
	if(_idEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No ids");
	}
	else
	{
		QStringList temp = _idEdit->text().split(",");
		QList<int> idList;
		for(int i=1; i<=temp.size(); i++)
		{
			idList.append(temp.at(i-1).toInt());
		}
		_requester->updateUserStatus(idList, _activeCheckBox->isChecked());
	}
}

void UserTestPanel::fillUser(TUserPointer user)
{
	_idEdit->setText(QString::number(user->getId()));
	_loginNameEdit->setText(user->getLoginName());
	_loginPasswordEdit->setText(user->getLoginPassword());
	_nameEdit->setText(user->getName());
	_emailEdit->setText(user->getEmail());
	_roleIdEdit->setText(QString::number(user->getRole()->getId()));
	_activeCheckBox->setChecked(user->getActive());
}

void UserTestPanel::requestFinished(bool error, QList<TUserPointer> users)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "List Success.");

		if(!_idEdit->text().isEmpty())
		{
			bool valid = true;
			int userId = _idEdit->text().toInt(&valid);
			if(valid)
			{
				for(int i=1; i<=users.size(); i++)
				{
					TUserPointer user = users.at(i-1);
					if(user->getId()==userId)
					{
						fillUser(user);
						break;
					}
				}
			}
		}
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "List Failure.");
	}
}

void UserTestPanel::createFinished(bool error, TUserPointer user)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Create Success.");
		fillUser(user);
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Create Failure.");
	}
}

void UserTestPanel::updateFinished(bool error, TUserPointer user)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Update Success.");
		fillUser(user);
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Update Failure.");
	}
}
